<script type="text/javascript">
var <?= $this->jsObject ?> = {
    opt: <?= $this->grid->invokeParams ?>,
	buttons: {}
};

(function($, $f) {
    $.extend($f, {
        expand: function(subgridId, rowId) {
            var subgridListId = subgridId + "_sub";
            var url = $f.opt.subgridUrl + "?" + $.param({id: rowId});
            var sendData = [];

            $("#"+ subgridId).html("<dl id='" + subgridListId + "'></dl>");
            $.getJSON(url, sendData, function(data) {
                if (!data || typeof data != 'object') {
                    return;
                }

                var list = $("#" + subgridListId);
                for (var item in data) {
                    $("<dt>" + data[item]["name"] + "</dt>").appendTo(list);
                    $("<dd>" + data[item]["value"] + "</dd>").appendTo(list);
                }
            });
        },
        collapse: function(subgridId, rowId) {
            return true;
        }
    });

})(jQuery, <?= $this->jsObject ?>);
</script>

<script type="text/javascript">
//навигация
var btns = <?= $this->jsObject ?>.buttons;
var dialogOpt = {
	width:"226px"
};

<? foreach ($this->gridButtons as $button => $options) : ?>
  <? if (!empty($options['active'])) : ?>
		<? if ($button == 'b_add') : ?>
			btns.add = {
				gridId: "#<?= $options['grid_id'] ?>",
				options: {
					buttonicon: "ui-icon-plus",
					caption: "<?= $options['caption'] ? $options['caption'] : '' ?>",
					title: "<?= $options['title'] ? $options['title'] : 'Добавить' ?>",
					position: "<?= $options['position'] ?>",
					onClickButton: function() {
						var q = $.param(<?= $options['query'] ?>);
                    	location.href = "<?= $options['path'] ?>?" + q;
					}
				}
			}
		<? endif ?>

		<? if ($button == 'b_edit') : ?>
			btns.edit = {
				gridId: "#<?= $options['grid_id'] ?>",
				options: {
					buttonicon: "ui-icon-pencil",
                    caption: "<?= $options['caption'] ? $options['caption'] : '' ?>",
					title: "<?= $options['title'] ? $options['title'] : 'Редактировать' ?>",
					position: "<?= $options['position'] ?>",
					onClickButton: function() {
						var rowId = $("#<?= $options['grid_id'] ?>").jqGrid("getGridParam", "selrow");
                        if (rowId > 0) {
                            var q = <?= $options['query'] ?>;
                            q.id = rowId;
                            q = $.param(q);
                            location.href = "<?= $options['path'] ?>?" + q;
                        } else {
                    	    info_dialog("Внимание", "Пожалуйста, выберите объект", null, dialogOpt);
                    	}
					}
				}
			}
		<? endif ?>

		<? if ($button == 'b_categories') : ?>
			btns.categories = {
				gridId: "#<?= $options['grid_id'] ?>",
				options: {
					buttonicon: "ui-icon-document",
                    caption: "<?= $options['caption'] ? $options['caption'] : 'Категории' ?>",
					title: "<?= $options['title'] ? $options['title'] : 'Показать категории объекта' ?>",
					position: "<?= $options['position'] ?>",
					onClickButton: function() {
						var rowId = $("#<?= $options['grid_id'] ?>").jqGrid("getGridParam", "selrow");
						if (rowId > 0) {
							var q = <?= $options['query'] ?>;
							q.id = rowId;
							q = $.param(q);
							location.href = "<?= $options['path'] ?>?" + q;
						} else {
							info_dialog("Внимание", "Пожалуйста, выберите объект", null, dialogOpt);
						}
					}
				}
			}
		<? endif ?>

		<? if ($button == 'b_objects' ): ?>
			btns.objects = {
				gridId: "#<?= $options['grid_id'] ?>",
				options: {
					buttonicon: "ui-icon-document",
                    caption: "<?= $options['caption'] ? $options['caption'] : 'Связи' ?>",
					title: "<?= $options['title'] ? $options['title'] : 'Показать связанные объекты' ?>",
					position: "<?= $options['position'] ?>",
					onClickButton: function() {
						var rowId = $("#<?= $options['grid_id'] ?>").jqGrid("getGridParam", "selrow");
                        if (rowId > 0) {
                            var q = <?= $options['query'] ?>;
                            q.nodeid = rowId;
                            q = $.param(q);
                            location.href = "<?= $options['path'] ?>?" + q;
                        } else {
                            info_dialog("Внимание", "Пожалуйста, выберите объект", null, dialogOpt);
                        }
					}
				}
			}
		<? endif ?>

        <? if ($button == 'b_select_objects' ): ?>
            btns.delLinks = {
                gridId: "#<?= $options['grid_id'] ?>",
				options: {
                    buttonicon: "ui-icon-arrow-2-e-w",
                    caption: "<?= $options['caption'] ? $options['caption'] : 'Редактировать связи' ?>",
					title: "<?= $options['title'] ? $options['title'] : 'Редактировать связи объекта' ?>",
					position: "<?= $options['position'] ?>",
					onClickButton: function() {
                        var grid = $("#<?= $options['grid_id'] ?>");
						var rowId = grid.jqGrid("getGridParam", "selrow");
                        if (rowId > 0) {
                            location.href = "<?= $options['path'] ?>?" + $.param($.extend(
                                <?= $options['query'] ?>, {id: rowId}
                            ));
                        } else {
                            info_dialog("Внимание", "Пожалуйста, выберите объект", null, dialogOpt);
                        }
                    }
                }
            }
        <? endif ?>
  <? endif ?>
<? endforeach ?>

$(document).ready(function() {
	for (var button in btns) {
		$("#<?= $this->grid->tableId ?>").jqGrid("navButtonAdd", "<?= $this->grid->pagerId ?>", btns[button].options);
	}

    
});	
</script>

<?= $this->grid ?>

<? if ($this->backurl) : ?>
<p>
    <a href="<?= $this->backurl ?>"><?= $this->backurlLabel ?></a>
</p>
<? endif ?>